package hu.rsc.feeds.client.gui.generated;

import hu.rsc.feeds.client.Feeds;
import hu.rsc.feeds.client.data.Dictionary;
import hu.rsc.feeds.client.data.SystemFeed;
import hu.rsc.feeds.client.data.UserInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class SystemForm extends Composite {
    public static final String HELP_URL = GWT.getHostPageBaseURL() + "rscrssfeeds/help?app=";
    public static final String NEWS_URL = GWT.getHostPageBaseURL() + "rscrssfeeds/whatisnew?app=";
    private SystemPanel parent;

    private FlexTable form = new FlexTable();

    private TextBox applicationTb;
    private CheckBox helpCb;
    private SystemFeed feed = new SystemFeed();
    final CheckBox huCb = new CheckBox();
    final CheckBox enCb = new CheckBox();

    public SystemForm(SystemPanel parent) {
	this.parent = parent;
	buildContent();
	initWidget(this.form);
    }

    public SystemFeed getFeed() {
	return this.feed;
    }

    public void refresh(SystemFeed feed) {
	this.feed = feed;
	buildContent();
    }

    private void buildContent() {
	this.form.clear();
	this.form.addStyleName("form");
	this.form.addStyleName("border");
	Label lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_APPLICATION, lb));
	this.form.setWidget(1, 0, lb);
	this.applicationTb = new TextBox();
	this.applicationTb.addStyleName("forminput");
	this.form.setWidget(1, 1, this.applicationTb);
	this.applicationTb.setValue(this.feed.getApplication());

	lb = new Label();
	lb.addStyleName("formlabel");
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_ISHELP, lb));
	this.form.setWidget(2, 0, lb);
	this.helpCb = new CheckBox();
	this.helpCb.addStyleName("forminput");
	this.form.setWidget(2, 1, this.helpCb);
	this.helpCb.setValue(this.feed.getHelp());

	lb = new Label();
	lb.setText(Dictionary.getEntry(Dictionary.LABEL_GEN_CHANNEL_URL, lb));
	this.form.setWidget(4, 0, lb);
	this.form.getFlexCellFormatter().setColSpan(4, 0, 2);

	TextBox urlTb = new TextBox();
	urlTb.setReadOnly(true);
	urlTb.setWidth("100%");
	Long fid = this.feed.getId();
	if (fid == null || fid < 0) {
	    urlTb.setText("N/A");
	} else {
	    if (this.feed.getHelp()) {
		urlTb.setText(HELP_URL + this.feed.getApplication());
	    } else {
		urlTb.setText(NEWS_URL + this.feed.getApplication());
	    }
	}
	this.form.setWidget(5, 0, urlTb);
	this.form.getFlexCellFormatter().setColSpan(5, 0, 2);

	String lang = Feeds.getLanguage();

	Label langLb = new Label();
	langLb.addStyleName("formtitle");
	langLb.setText(Dictionary.getEntry(Dictionary.LABEL_LANG, langLb));
	this.form.setWidget(6, 0, langLb);
	this.form.getFlexCellFormatter().setColSpan(6, 0, 2);

	this.enCb.setText(Dictionary.getEntry(Dictionary.LABEL_LANG_EN, this.enCb));
	if (lang.equals(UserInfo.LANG_EN)) {
	    this.enCb.setValue(true);
	}
	this.form.setWidget(7, 0, this.enCb);

	this.huCb.setText(Dictionary.getEntry(Dictionary.LABEL_LANG_HU, this.huCb));
	if (lang.equals(UserInfo.LANG_HU)) {
	    this.huCb.setValue(true);
	}
	this.form.setWidget(7, 1, this.huCb);

	this.enCb.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		if (SystemForm.this.enCb.getValue()) {
		    SystemForm.this.huCb.setValue(false);
		    SystemForm.this.feed.setLanguage(UserInfo.LANG_EN);
		}
	    }
	});
	this.huCb.addClickHandler(new ClickHandler() {
	    @Override
	    public void onClick(ClickEvent event) {
		if (SystemForm.this.huCb.getValue()) {
		    SystemForm.this.enCb.setValue(false);
		    SystemForm.this.feed.setLanguage(UserInfo.LANG_HU);
		}
	    }
	});

	if (Feeds.getUserInfo() != null) {
	    this.enCb.setValue(false);
	    this.huCb.setValue(false);
	    if (this.feed.getLanguage() == null) {
		this.enCb.setValue(true);
		SystemForm.this.feed.setLanguage(UserInfo.LANG_EN);
	    }
	    if (this.feed.getLanguage().equals("en")) {
		this.enCb.setValue(true);
	    } else if (this.feed.getLanguage().equals("hu")) {
		this.huCb.setValue(true);
	    } else {
		this.enCb.setValue(true);
	    }
	}

	HorizontalPanel buttonPanel = new HorizontalPanel();
	buttonPanel.addStyleName("buttonpanel");
	this.form.setWidget(8, 0, buttonPanel);
	this.form.getFlexCellFormatter().setColSpan(8, 0, 2);

	Button save = new Button();
	save.setText(Dictionary.getEntry(Dictionary.BUTTON_EDIT_PROFILE, save));
	buttonPanel.add(save);

	save.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		if (validate()) {
		    SystemForm.this.parent.save(SystemForm.this.feed);
		}
	    }
	});

	Button newBt = new Button();
	newBt.setText(Dictionary.getEntry(Dictionary.BUTTON_NEW_SYSTEM_CHANEL, save));
	buttonPanel.add(newBt);
	newBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		refresh(new SystemFeed());
	    }
	});

	Button delBt = new Button();
	delBt.setText(Dictionary.getEntry(Dictionary.BUTTON_DELETE, save));
	delBt.addClickHandler(new ClickHandler() {

	    @Override
	    public void onClick(ClickEvent event) {
		Feeds.getService().deleteGeneratedSystemFeed(SystemForm.this.feed.getId(), new AsyncCallback<String>() {

		    @Override
		    public void onFailure(Throwable caught) {
			Window.alert(Dictionary.getEntry(Dictionary.MESSAGE_COMM_ERROR, null));
		    }

		    @Override
		    public void onSuccess(String result) {
			if (result != null) {
			    Window.alert(Dictionary.getEntry(result, null));
			} else {
			    refresh(new SystemFeed());
			    SystemForm.this.parent.refreshForm(new SystemFeed());
			    SystemForm.this.parent.refreshFeedsTable();
			}
		    }
		});
	    }
	});
	buttonPanel.add(delBt);
	buttonPanel.add(delBt);

    }

    private boolean validate() {
	this.feed.setApplication(this.applicationTb.getValue());
	this.feed.setHelp(this.helpCb.getValue());
	StringBuilder error = new StringBuilder();

	if (this.applicationTb.getValue() == null || this.applicationTb.getValue().length() < 1) {
	    error.append(Dictionary.getEntry(Dictionary.MESSAGE_TILE_REQUIRED, null)).append("\r\n");
	}

	if (error.length() > 0) {
	    Window.alert(error.toString());
	    return false;
	}
	return true;
    }
}
